<?php

namespace App\Models\Chain\Invite;

use App\Models\Chain\AbstractHandler;
use Illuminate\Support\Facades\DB;
use App\Helpers\Logger\SLogger;

/**
 *
 */
class DoInviteHandler extends AbstractHandler
{
    #外部传参

    private $params = array();

    public function __construct($params)
    {
        $this->params = $params;
        $this->setSuccessor($this);
    }

    /**
     * @return mixed]
     */
    //1.判断邀请码是否存在
    //2.记录用户邀请日志(通过邀请记录日志,判断用户邀请个数)
    //3.更新用户积分表
    //4.用户积分日志表
    public function handleRequest()
    {
	    $result = ['error' => '出错啦', 'code' => 10001];

	    DB::beginTransaction();
	    try
	    {
		    $this->setSuccessor(new CheckUserInviteAction($this->params));
		    $result = $this->getSuccessor()->handleRequest();
		    if (isset($result['error'])) {
//			    DB::rollback();

			    SLogger::getStream()->error('注册邀请失败-try');
			    SLogger::getStream()->error($result['error']);
		    }
		    else
		    {
			    DB::commit();
                SLogger::getStream()->info('注册邀请成功');

            }
	    }
	    catch (\Exception $e)
	    {
		    DB::rollBack();

		    SLogger::getStream()->error('注册邀请失败-catch');
		    SLogger::getStream()->error($e->getMessage());
	    }
	    return $result;
    }

}
